package com.example.pingze.controller;

import com.example.pingze.entity.MyInfo;
import com.example.pingze.entity.UserLogin;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.LoginService;
import com.example.pingze.service.MyInfoService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/start")
@CrossOrigin(origins = "*")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private MyInfoService myInfoService;

    //忘记密码
    @PostMapping("/forgetPassword")
    public Result<String> forgetPassword(@RequestBody UserLogin userLogin){
        if(StringUtils.isEmpty(userLogin.getAccount())){
            throw new PingZeException("账号不能为空");
        }
        if (StringUtils.isEmpty(userLogin.getPassword())){
            throw new PingZeException("新密码不能设置为空");
        }
        String message = loginService.update(userLogin);
        return ResultVOUtils.success(message);
    }

    //用户登录
    @PostMapping("/login")
    public Result<String> login(@RequestBody UserLogin userLogin){
        if (StringUtils.isEmpty(userLogin.getAccount())) {
            throw new PingZeException("登录账号不能为空");
        }
        if (StringUtils.isEmpty(userLogin.getPassword())) {
            throw new PingZeException("登录密码不能为空");
        }
        //返回带有token的map集合
        String s = loginService.login(userLogin);
        return ResultVOUtils.result(s);
    }

    //注册账户
    @PostMapping("/register")
    public Result<String> register(@RequestBody UserLogin userLogin) throws IOException {
        if (StringUtils.isEmpty(userLogin.getAccount())) {
            throw new PingZeException("登录账号不能为空");
        }
        if(StringUtils.isEmpty(userLogin.getPassword())){
            throw new PingZeException("密码不能为空");
        }
        UserLogin add = loginService.findUserLoginByAccount(userLogin.getAccount());
        if(add != null){
            throw new PingZeException("账号已经被注册，请输入新的账号");
        }else{
            String uuid = UUID.randomUUID().toString();
            userLogin.setId(uuid);
            //将注册信息放在login_user表中
            loginService.register(userLogin);
            //将注册账号放在my_info表中
            myInfoService.addMyInfo(userLogin.getAccount());
        }
        return ResultVOUtils.success("注册成功");
    }
    //注销账户
    @PostMapping("/logout")
    public Result<String> logout(@RequestBody UserLogin userLogin){

        if(userLogin != null){
            String account = userLogin.getAccount();
            UserLogin find = loginService.findUserLoginByAccount(account);
            MyInfo myInfo = myInfoService.findMyInfoByAccount(account);
            if(find != null && myInfo != null){
                loginService.delete(find);
                myInfoService.delete(myInfo);
            }else{
                throw new PingZeException("没有该账号，注销失败");
            }
        }
        return ResultVOUtils.success("注销成功");
    }

    /**
     * 后台管理系统
     * userLogin数据的增删改查
     * @param userLogin
     * @param pageCode
     * @param pageSize
     * @return
     */
    @CrossOrigin
    @PostMapping ("/page")
    public Result<Page<UserLogin>> getList(
            UserLogin userLogin,
            @RequestParam(name="pageCode",required = false,defaultValue = "1") Integer pageCode,
            @RequestParam(name="pageSize",required = false, defaultValue = "2") Integer pageSize
    ){
        if(pageCode < 1){
            throw new PingZeException("页码不能小于1");
        }
        Sort.Order accountOrder = Sort.Order.asc("account");
        Sort sort = Sort.by(accountOrder);
        Pageable pageable = PageRequest.of(pageCode - 1, pageSize, sort);
        Page page = loginService.getUserLoginList(userLogin,pageable);
        return ResultVOUtils.result(page);
    }
    @CrossOrigin
    @PostMapping("/add")
    public Result<UserLogin> addUsers(String account,String password) throws IOException {
        if(account == null){
            throw new PingZeException("账号为空");
        }
        if(password == null){
            throw new PingZeException("密码为空");
        }
        UserLogin userLogin = new UserLogin();
        userLogin.setAccount(account);
        userLogin.setPassword(password);
        UserLogin add = loginService.add(userLogin);
        if(add != null){
            myInfoService.addMyInfo(account);
        }
        return ResultVOUtils.result(add);
    }
    @CrossOrigin
    @PostMapping("/edit")
    public Result<UserLogin> editUser(String account, String password){
        if(account == null){
            throw new PingZeException("账号为空");
        }
        if(password == null){
            throw new PingZeException("密码为空");
        }
        UserLogin edit = loginService.edit(account, password);
        return ResultVOUtils.result(edit);
    }
    @CrossOrigin
    @Transactional
    @PostMapping("/delete")
    public Result<String> deleteUser(String account){
        loginService.deleteUserLoginByAccount(account);
        MyInfo find = myInfoService.findMyInfoByAccount(account);
        if(find != null){
            myInfoService.delete(find);
        }
        return ResultVOUtils.success("用户删除成功");
    }
}
